{{- define "kube_flannel_resources" }}
cpu: 25m
memory: 50Mi
{{- end }}

{{- if (.Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
---
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: flannel
  namespace: d8-{{ .Chart.Name }}
  {{- include "helm_lib_module_labels" (list . (dict "app" "flannel" "workload-resource-policy.deckhouse.io" "every-node")) | nindent 2 }}
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind: DaemonSet
    name: flannel
  updatePolicy:
    updateMode: "Initial"
  resourcePolicy:
    containerPolicies:
    - containerName: "kube-flannel"
      minAllowed:
        {{- include "kube_flannel_resources" . | nindent 8 }}
      maxAllowed:
        cpu: 50m
        memory: 50Mi
{{- end }}
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: flannel
  namespace: d8-{{ .Chart.Name }}
  {{- include "helm_lib_module_labels" (list . (dict "app" "flannel")) | nindent 2 }}
spec:
  updateStrategy:
    type: RollingUpdate
  selector:
    matchLabels:
      app: flannel
  template:
    metadata:
      labels:
        tier: node
        app: flannel
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
    spec:
      # https://github.com/coreos/flannel/issues/1002 workaround
      terminationGracePeriodSeconds: 0
      imagePullSecrets:
      - name: deckhouse-registry
      {{- include "helm_lib_priority_class" (tuple . "system-node-critical") | nindent 6 }}
      {{- include "helm_lib_tolerations" (tuple . "any-node" "with-uninitialized") | nindent 6 }}
      {{- include "helm_lib_module_pod_security_context_run_as_user_root" . | nindent 6 }}
      hostNetwork: true
      dnsPolicy: ClusterFirstWithHostNet
      serviceAccountName: {{ .Chart.Name }}
      containers:
      - name: kube-flannel
        {{- include "helm_lib_module_container_security_context_capabilities_drop_all_and_add" (list . (list "NET_ADMIN" "NET_RAW")) | nindent 8 }}
        image: {{ include "helm_lib_module_image" (list . "flanneld") }}
        args:
        - --ip-masq
        - --kube-subnet-mgr
        env:
        - name: POD_NETWORK_MODE
          value: {{ .Values.cniFlannel.internal.podNetworkMode }}
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        volumeMounts:
        - name: run
          mountPath: /run
        - name: flannel-cfg
          mountPath: /etc/kube-flannel/
        - name: cni
          mountPath: /etc/cni/net.d
        - name: tmp
          mountPath: /tmp
        resources:
          requests:
            {{- include "helm_lib_module_ephemeral_storage_logs_with_extra" 10 | nindent 12 }}
{{- if not (.Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
            {{- include "kube_flannel_resources" . | nindent 12 }}
{{- end }}
      volumes:
        - name: run
          hostPath:
            path: /run
        - name: cni
          hostPath:
            path: /etc/cni/net.d
        - name: flannel-cfg
          configMap:
            name: flannel
        - name: tmp
          emptyDir: {}
